﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;

namespace bmapi.Controllers
{
    public class CAD34Controller : CADFinishController
    {
        [HttpPost, Route("cad34/Finish")]
        public string Finish(dynamic json)
        {
            int pcid = Convert.ToInt32(json.id);
            try
            {
                User user = GetUserByLoginID(GetCrrentIDByToken());
                if (user == null)
                    return ReturnError(201, "当前工号不存在");

                WriteDebug("cad34/Finish", user.login_id.ToString() + "pcid:" + pcid);

                if (0 != CanFinishTask(user.login_id, 34, pcid))
                    return ReturnError(202, "无权结束本工序");
                WriteDebug("cad34/Finish","继续");
                Process next = GetNextProcess(user.PLID, 34);
                WriteDebug("cad34/Finish", "nextcode:"+next.code);
                if (next.code == 35)
                    return AddPod(pcid, user);
                else
                {
                    //下一道工序是排料
                    if (1 == ChangeProcessState(34, pcid))
                        return ReturnError(200, "OK");
                    else
                    {
                        return ReturnError(203, "改变流程状态失败");
                    }
                }
            }
            catch (Exception e)
            {
                Directory.Delete(GetPCPath(pcid));
                return ReturnError(204, e.Message);
            }
        }

        protected string AddPod(int pcid, User user)
        {
            WriteDebug("AddPod", "1 addpod");
            PC pc = GetPC(pcid);
            if(pc==null)
                return ReturnError(201, "不存在流程");
            string podPath = GetPCPath(pc.path) + "\\pod";
            if (!Directory.Exists(podPath))
            {
                WriteDebug("AddPod", "当前pod文件路径" + podPath + "不存在");
                return ReturnError(205, "当前pod文件路径" + podPath + "不存在");
            }
            string sql = "insert into Pod_tb values(@id,@pcid,@podname,0,@time,0,' ')";
            List<SQLCommand> cmds = new List<SQLCommand>();

            //查找需要检查的pod文件
            DirectoryInfo folder = new DirectoryInfo(podPath);
            SQLCommand sc = null;
            foreach (FileInfo item in folder.GetFiles("*#.pod"))
            {
                
                string podname = item.FullName; //文件名的形式是: 流程名[数字]#.pod 
                int left = podname.IndexOf('[');
                int right = podname.IndexOf(']');
                int pod = int.Parse(podname.Substring(left + 1, right - left - 1));

                int pid = GetNewSafeID(user.login_id);

                sc = new SQLCommand(sql);
                sc.Add("@id", pid);
                sc.Add("@pcid", pcid);//流程ID与OrderID一致所以不需要去Order_tb表获取id
                sc.Add("@podname", pod);
                sc.Add("@time", DateTime.Now);
                cmds.Add(sc);
            }
            if (cmds.Count > 0)
            {
                if (SQLCommand.ExecTran(cmds) == 1)
                {
                    //生成记录成功,流程状态改为 35
                    //35是个很特别的工序,因为没有人申请这个流程,而是申请pod 
                    pc.download = 0;
                    pc.station = 0;
                    pc.step = 35;
                    if (1 == ChangeProcessState(pc))
                    {
                        WriteDebug("AddPod", "生成记录成功,流程状态改为 35");
                        return ReturnError(200, "OK");
                    }
                    else
                    {
                        Directory.Delete(podPath);//0418 流程状态改变失败  删除pod目录
                        return ReturnError(206, "改变流程状态失败");
                    }
                }
                else
                    //数据生成失败, 直接跳到排料环节
                    return ReturnError(201, "pod数据录入出错, 请在排料的时候人工检查");
            }
            else
            {
                WriteDebug("AddPod", "没有需要检查的特体文件");
                //没有需要检查的文件,直接跳到排料环节   当前工序
                if (1 == ChangeProcessState(35, pcid))
                    return ReturnError(200, "OK");
                else
                    return ReturnError(207, "改变流程状态失败");
            }
        }


        private void SetPC()
        {

        }
    }
}
